<?php
/**
 * Class that operate on table 'hs_activity_activityTag'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsActivityActivityTagMySqlDAO implements HsActivityActivityTagDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsActivityActivityTagMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_activity_activityTag WHERE activity_activityTagID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_activity_activityTag';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_activity_activityTag ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsActivityActivityTag primary key
 	 */
	public function delete($activity_activityTagID){
		$sql = 'DELETE FROM hs_activity_activityTag WHERE activity_activityTagID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($activity_activityTagID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsActivityActivityTagMySql hsActivityActivityTag
 	 */
	public function insert($hsActivityActivityTag){
		$sql = 'INSERT INTO hs_activity_activityTag (activityID, activityTagID) VALUES (?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsActivityActivityTag->activityID);
		$sqlQuery->set($hsActivityActivityTag->activityTagID);

		$id = $this->executeInsert($sqlQuery);	
		$hsActivityActivityTag->activityActivityTagID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsActivityActivityTagMySql hsActivityActivityTag
 	 */
	public function update($hsActivityActivityTag){
		$sql = 'UPDATE hs_activity_activityTag SET activityID = ?, activityTagID = ? WHERE activity_activityTagID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsActivityActivityTag->activityID);
		$sqlQuery->set($hsActivityActivityTag->activityTagID);

		$sqlQuery->set($hsActivityActivityTag->activityActivityTagID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_activity_activityTag';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByActivityID($value){
		$sql = 'SELECT * FROM hs_activity_activityTag WHERE activityID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityTagID($value){
		$sql = 'SELECT * FROM hs_activity_activityTag WHERE activityTagID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByActivityID($value){
		$sql = 'DELETE FROM hs_activity_activityTag WHERE activityID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityTagID($value){
		$sql = 'DELETE FROM hs_activity_activityTag WHERE activityTagID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsActivityActivityTagMySql 
	 */
	protected function readRow($row){
		$hsActivityActivityTag = new HsActivityActivityTag();
		
		$hsActivityActivityTag->activityActivityTagID = $row['activity_activityTagID'];
		$hsActivityActivityTag->activityID = $row['activityID'];
		$hsActivityActivityTag->activityTagID = $row['activityTagID'];

		return $hsActivityActivityTag;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsActivityActivityTagMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>